package org.rasterfun.utils

object Rectangle {
  def apply(x: Float, y: Float, width: Float, height: Float): Rectangle = new RectangleImpl(x, y, width, height)
  def apply(width: Float, height: Float): Rectangle = new RectangleImpl(0, 0, width, height)
}

/**
 * Represents an axis aligned rectangular shaoe
 * 
 * @author Hans Haggstrom
 */
trait Rectangle {
  def x : Float
  def y : Float
  def width : Float
  def height : Float

  final def x2 = x + width
  final def y2 = y + height

  final def centerX = x + width * 0.5f
  final def centerY = y + height * 0.5f

  def containsPoint( px : Float, py : Float ) : Boolean = px >= x && py >= y && px < x2 && py < y2

  def union(other : Rectangle) : Rectangle = {
    val x1 = x min other.x
    val y1 = y min other.y
    val nw = (x2 max other.x2) - x1
    val nh = (y2 max other.y2) - y1
    new RectangleImpl( x1, y1, nw, nh )
  }
}


